<template>
    <scroll-view class="maintain-hall-list" scroll-y @scrolltolower="scroll" :refresher-triggered="triggered"
        :refresher-threshold="100" @refresherrestore="onRestore" refresher-enabled @refresherrefresh="onRefresh"
        :ower-threshold="0">
        <view class="maintain-hall-list-item" :class="'type-' + item.approveStatus" v-for="(item, index) in data"
            :key="index">
            <view class="header">
                <view class="item-dt">
                    <view class="dt-l">
                        <p class="name u-line-1">{{ item.deviceNo }}</p>
                        <p class="name u-line-1">{{ item.deviceName }}</p>
                    </view>
                    <view class="dt-r">
                        <u-tag type="success" text="待分配" v-if="navIndex == 0"></u-tag>
                        <u-tag type="success" text="保养中" v-if="navIndex == 2"></u-tag>
                        <u-tag type="success" text="待确认" v-if="navIndex == 3"></u-tag>
                        <u-tag type="success" text="已结单" v-if="navIndex == 4"></u-tag>
                        <u-tag type="success" text="已过期" v-if="navIndex == 5"></u-tag>
                    </view>
                </view>
                <view class="type-list">
                    <view class="type-list-item" style="background-color: rgb(255, 241, 240); color: rgb(224, 62, 85);">
                        {{ item.taskLevel?`${['一','二','三','四','五','六'][item.taskLevel-1]}级保养`:'--' }}
                    </view>
                    <view class="type-list-item"
                        style="background-color: rgb(241, 247, 253); color: rgb(24, 123, 222);">
                        {{ ['', '日', '周', '月', '季', '半年', '年', '班次', '计时'][item.triggerCycle || 0] }}
                    </view>
                    <view class="type-list-item"
                        style="background-color: rgb(241, 247, 253); color: rgb(24, 123, 222);">
                        {{ ['不需要确认', '需要确认'][item.isConfirm] }}
                    </view>
                </view>
            </view>

            <view class="main-box">
                <view class="item-dd" v-if="item.taskStatus > 0 && item.taskStatus < 4">
                    <text class="label">保养人:</text>
                    <text class="value">{{ item.operateName }}</text>
                </view>
                <view class="item-dd">
                    <text class="label">计划名称:</text>
                    <text class="value">{{ item.planName }}</text>
                </view>
                <view class="item-dd">
                    <text class="label">保养单号:</text>
                    <text class="value">{{ item.maintenanceNumber }}</text>
                </view>
                <view class="item-dd">
                    <text class="label">具体位置:</text>
                    <text class="value">{{ item.functionalLocationName }}</text>
                </view>
                <view class="item-dd">
                    <text class="label">标准工时:</text>
                    <text class="value">{{ item.itemHours }}(min)</text>
                </view>

                <view class="item-dd">
                    <text class="label">创建时间:</text>
                    <text class="value">{{ item.createTime }}</text>
                </view>
                <view class="item-dd">
                    <text class="label">截止时间:</text>
                    <text class="value">{{ item.taskEndTime }}</text>
                </view>
            </view>

            <view class="footer-btn">
                <template v-if="navIndex == 0">
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainDel')" @click="handleDelete(item)" text="删除"></u-button>
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainDispath')" @click="handleDispath()" text="分配"></u-button>
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainService')" @click="handleService(item)" text="接单"></u-button>
                </template>
                <template v-if="navIndex == 2">
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainComplete')" @click="handleDone(item)" text="完成保养"></u-button>
                </template>
                <template v-if="navIndex == 3">
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainConfirm')" @click="handleAccept(item)" text="完成确认"></u-button>
                </template>
                <template v-if="navIndex == 5">
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainDel')" @click="handleDelete(item)" text="删除"></u-button>
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainDispath')" @click="handleDispath(item)" text="分配"></u-button>
                    <u-button class="btn" type="primary" v-if="$has('deviceMaintainService')" @click="handleService(item)" text="接单"></u-button>
                </template>
            </view>
        </view>
        <view class="loading animation-scale-up" v-if="loading"></view>
        <view class="scroll-no-data" v-if="scrollNoData">已拉到最底部</view>
        <MyModal v-model="modalShow" :title="modalTitle" :content="modalContent" @confirm="modalConfirm"
            :showCancelButton="true" @cancel="modalShow = false"></MyModal>
    </scroll-view>
</template>

<script>
    import MyModal from "@/components/myModal";
    import fetch from "@/utils/fetch";
    import {
        setPageData
    } from "@/store/user";
    export default {
        components: {
            MyModal,
        },
        props: {
            data: {
                type: Array,
                default: [],
            },
            scrollNoData: {
                type: Boolean,
                default: false,
            },
            navIndex: {
                type: Number,
                default: 0,
            },
        },

        data() {
            return {
                loading: true,
                triggered: false,
                noData: false,

                modalShow: false,
                modalTitle: "",
                modalContent: "",
            };
        },
        onPullDownRefresh() {
            uni.stopPullDownRefresh();
        },
        methods: {
            //删除
            handleDelete(item) {
                const that = this;
                this.modalShow = true;
                this.modalTitle = "删除提醒";
                this.modalContent = "是否删除!";
                this.modelFetchOption = {
                    method: 'delete',
                    url: '/dms/dcmMaintainTask/deleteBatch',
                    params: {
                        ids: item.id
                    },
                    data: {},
                    success() {
                        that.$emit('onRefresh');
                    }
                };
            },
            //分配
            handleDispath(item) {
                uni.navigateTo({
                    url: "/pages/device/maintain-hall/operator?multiple=false&id="+item.id,
                    animationType: "pop-in",
                });
            },
            //接单
            handleService(item) {
                const that = this;
                this.modalShow = true;
                this.modalTitle = "接单提醒";
                this.modalContent = "是否接单请确认!";
                this.modelFetchOption = {
                    method: 'post',
                    url: '/dms/dcmMaintainTask/distribute',
                    params: {},
                    data: {
                        id: item.id
                    },
                    success() {
                        that.$emit('onRefresh');
                    }
                };
            },
            //完成保养
            handleDone(item) {
                const that = this;
                uni.navigateTo({
                    url: "/pages/device/maintain-hall/maintain-completed?id=" +
                        item.id +
                        "&status=" +
                        that.navIndex,
                    animationType: "pop-in",
                });
            },
            //完成确认
            handleAccept(order) {
                const that = this;
                setPageData(order);
                uni.navigateTo({
                    url: "/pages/device/maintain-hall/accept?id=" +
                        order.id +
                        "&status=" +
                        that.navIndex,
                    animationType: "pop-in",
                });
            },
            //公共方法
            modalConfirm() {
                const that = this;
                that.modalShow = false;
                that.$loading.open();
                fetch(that.modelFetchOption.method, that.modelFetchOption.url, that.modelFetchOption.data, that
                    .modelFetchOption.params).then((res) => {
                    uni.$emit("openToast", {
                        type: "success",
                        title: "操作成功",
                        complete() {
                            that.modelFetchOption.success.call();
                        },
                    });
                });
            },
            onRefresh() {
                if (this._freshing) return;
                this._freshing = true;
                this.triggered = true;
                this.$emit("onRefresh");
                setTimeout(() => {
                    this.triggered = false;
                    this._freshing = false;
                }, 1000);
            },
            scroll(e) {
                const that = this;
                if (!that.noData) {
                    this.loading = true;
                    this.$emit("scrollBottom", (val) => {
                        that.noData = val;
                        setTimeout(() => {
                            this.loading = false;
                        }, 400);
                    });
                }
            },
            onRefresh() {
                if (this._freshing) return;
                this._freshing = true;
                this.triggered = true;
                this.$emit("onRefresh");
                setTimeout(() => {
                    this.triggered = false;
                    this._freshing = false;
                }, 1000);
            },
            onRestore() {
                this.triggered = "restore"; // 需要重置
            },
        },
    };
</script>

<style lang="scss" scoped>
    .maintain-hall-list {
        height: 100%;
        padding: 0 20rpx;

        .maintain-hall-list-item {
            padding-top: 16rpx;
            background-color: #fff;
            box-shadow: 0rpx 5rpx 13rpx 1rpx #e0e0e0;
            margin-bottom: 20rpx;
            border-radius: 10px;
            overflow: hidden;

            .item-dt {
                display: flex;
                align-items: flex-start;
                justify-content: space-between;
                margin-bottom: 27rpx;

                .name {
                    font-size: 18px;
                }

                .view {
                    color: #83b4ff;
                }

                .dt-l {
                    flex: 1;
                    margin-right: 10px;
                }

                .dt-r {
                    min-width: 70px;
                    text-align: center;
                    display: flex;
                    justify-content: center;
                }
            }

            .type-list {
                display: flex;

                .type-list-item {
                    font-size: 14px;
                    padding: 3px 10px;
                    border: 1px solid #e0e0e0;
                    margin-right: 10px;
                    border-radius: 5px;
                }

                .color0 {
                    background-color: #f7cccc;
                    color: red;
                }
            }

            .header {
                padding: 0 30rpx 10px 30rpx;
                border-bottom: 1px solid #e0e0e0;
            }

            .main-box {
                padding: 20rpx 30rpx;
                border-bottom: 1px solid #e0e0e0;

                .item-dd {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    overflow: hidden;
                    margin-bottom: 22rpx;

                    .label {
                        margin-right: 20rpx;
                        font-size: 28rpx;
                        width: 120rpx;
                        min-width: 120rpx;
                    }

                    .value {
                        font-size: 28rpx;
                        flex: 1;
                        overflow: hidden;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        color: #666;
                    }

                    .btn {
                        width: 170rpx;
                        min-width: 170rpx;
                        margin-left: 29rpx;
                        height: 60rpx;
                        color: #fff;
                        font-size: 24rpx;
                    }
                }
            }
        }

        .img-box {
            width: 120rpx;
            margin-bottom: 27rpx;
        }

        .footer-btn {
            display: flex;
            padding: 20rpx;
            align-items: flex-end;
            justify-content: end;

            .btn {
                width: 80px;
                margin: 0;
                height: 32px;
                line-height: 32px;
                margin-right: 10rpx;

                &:last-child {
                    margin-right: 0;
                }
            }
        }

        .scroll-no-data {
            text-align: center;
        }
    }
</style>